// order-detail.js
var app = getApp();
var iFDU = require('../../utils/iFDU.js');
Page({

  data: {
    config: app.config,
    globalData: app.globalData,

    orderStatus: {
      1: '待支付',
      2: '全速定制中...',
      3: '已发货', 
      5: '已完成',
      13: '用户取消',
      15: '系统取消'
    },

    invoiceTypeName: {
      0: '不开发票', 
      1: '增值税普通发票', 
      2: '增值税专用发票'
    },

    payTypeName: {
      1: '微信', 
      2: '余额', 
      3: '余额 + 微信', 
      4: '支付宝', 
      5: '余额 + 支付宝',
      6: '提货码'
    },

    orderId: 0,
    orderDetail: {}
  },

  onLoad: function (options) {
    var that = this;
    that.data.orderId = options.orderId;
  },

  onShow: function () {
    var that = this;
    
    app.getUserInfo(function (globalData) {
      that.setData({
        globalData: globalData
      });

      iFDU.api.doGet(
        'order/getOrderInfo',
        {
          userId: globalData.userId,
          orderId: that.data.orderId
        },
        function (data) {
          that.setData({
            orderDetail: data.orderView
          });
        }
      );
    });


    
  },

  showCSTips: function () {
    wx.showModal({
      content: '如果您需要联系我们或售后问题，请在服务号中发起会话，或添加逸见服饰小秘书微信进行及时沟通！微信号：yjshirt',
      showCancel: false,
      confirmText: "知道了",
      success: function (res) {
        if (res.confirm) {
          console.log('用户点击确定')
        }
      }
    });
  },

  deleteOrder: function (e) {
    var that = this;
    wx.showModal({
      title: '删除订单',
      content: '将此订单从列表中永久删除吗？',
      confirmText: "删除",
      cancelText: "保留",
      success: function (res) {
        if (res.confirm) {
          iFDU.api.doGet(
            'order/deleteOrder',
            {
              userId: that.data.globalData.userId,
              orderId: e.currentTarget.dataset.id,
            },
            function (data) {
              wx.navigateBack({

              });
            }
          );
        }
      }
    });
  },

  cancelOrder: function (e) {
    var that = this;
    wx.showModal({
      title: '取消订单',
      content: '确定取消此订单吗？',
      confirmText: "取消订单",
      cancelText: "先不取消",
      success: function (res) {
        if (res.confirm) {
          iFDU.api.doGet(
            'order/cancelOrder',
            {
              userId: that.data.globalData.userId,
              orderId: e.currentTarget.dataset.id,
            },
            function (data) {
              iFDU.api.doGet(
                'order/getOrderInfo',
                {
                  userId: that.data.globalData.userId,
                  orderId: that.data.orderId
                },
                function (data) {
                  var orderDetail = data.orderView;
                  var orderItems = orderDetail.orderDetailViewList;
                  var needCustomize = false;
                  if (orderItems && orderItems.length > 0) {
                    for (var j = 0; j < orderItems.length; j++) {
                      var orderItem = orderItems[j];
                      var product = orderItem.mainProductView;
                      if (product && product.is_virtual == 0 && product.is_broider == 1) {
                        needCustomize = true;
                        break;
                      }
                    }
                  }
                  orderDetail.needCustomize = needCustomize;
                  that.setData({
                    orderDetail: orderDetail
                  });
                }
              );
            }
          );
        }
      }
    });
  },

  viewExpress: function (e) {
    var deliverCompany = e.currentTarget.dataset.deliver_company;
    var deliverOrderId = e.currentTarget.dataset.deliver_order_id;
    wx.navigateTo({
      url: '../trace-detail/trace-detail?deliverCompany=' + deliverCompany + '&deliverOrderId=' + deliverOrderId,
    })
  },

  payOrder: function (e) {
    var that = this;
    var orderId = e.currentTarget.dataset.id;
    var data = {
      userId: that.data.globalData.userId,
      orderId: orderId,
    };
    iFDU.api.doGet('payment/getInfoForPayment', data, function (data) {
      wx.navigateTo({
        url: '../goods-pay/goods-pay?orderId=' + orderId,
      })
    });
  },

  goHome: function () {
    wx.switchTab({
      url: '../home/home',
    })
  },

  onShareAppMessage: function (res) {
    var that = this;
    return {
      title: '爱上衬衫，从「逸见」开始…',
      imageUrl: 'http://s.i-fdu.com/runto/wxa/s/yjshare.jpeg',
      path: 'pages/home/home',
      success: function (res) {
      },
      fail: function (res) {
      }
    }
  }
})